const { feishu, jenkinsEndTemplate } = require("./index");

const notifyMobile = "17718139319"; /** 通知的用户手机号 */

const build = feishu.use("build");

const ensureAuth = build.ensureTenantAuth();
const ensureOpenId = build.getUserId(notifyMobile).then(
  ({
    data: {
      data: { user_list },
    },
  }) => user_list[0].user_id
);
module.exports.sendAppMessage = async function (message) {
  await ensureAuth;
  const openId = await ensureOpenId;

  return build.sendAppMessage(openId, message);
};

async function resolveImageKey(url) {
  return build.uploadImage(url).then(
    ({
      data: {
        data: { image_key },
      },
    }) => image_key
  );
}

module.exports.sendAppImageMessage = async function (imageUrl) {
  await ensureAuth;
  const openId = await ensureOpenId;
  const imageKey = await resolveImageKey(imageUrl);

  return build.sendAppImageMessage(openId, imageKey);
};

module.exports.sendAppMultiLineMessage = async function (title, messages) {
  await ensureAuth;
  const openId = await ensureOpenId;

  return build.sendAppMultiLineMessage(openId, title, messages);
};